home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: returning a string from a function
- Date: 12 Jan 1996 11:40:15 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4d5hav$6mt@sparcserver.lrz-muenchen.de>
- References: <4d4uh8$q46@mailhost.mwmicro.com>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- aschlies@citynet.net (Tony Schliesser) writes:
-
- >I am learning C with the aid of only a book. I am at an inpass in
- >this process. I have a small function that needs to return a string
- >back to the main routine. I have the following prototype:
-
- >char function_name(char in_string[80])
-
- This function will return a char, not a pointer to char or an array of
- char.
-
- Since returning an array of char is probably not what you want,
- returning a pointer to the first character of a string ('\0'-
- terminated sequence of characters) might be a good idea.
-
- >{
- > char value[80];
-
- > ...value takes on part of the value of the last 10 characters
- > of in_string.
-
- > return(value);
-
- 1) You should not return a pointer to an automatic object, and this
- is what you are trying to do. A simple solution might be to
- declare value as
-
- static char value[11];
-
- since you will need 11 chars to store the last 10 characters
- of a string as a string.
-
- >}
-
-
- >jWhen this function is done, it only returns the first character. I
- >"watched" the program execution and it shows that value indeed has the
- >last 10 characters. Any clues as to why the calling routine only gets
- >the one character??
-
- Because the function is defined to return a "char", not a "char *".
-
- >Also, I wrote the routine that copies the last 10 characters. Is there
- >a lib. function that does that for me?? Did I reinvent the wheel?
-
- Did you use strcpy()? AFAIK, there is no function that meets your
- specification in the standard C library.
-
- Kurt
-
-